/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ruoyi.flow.core;

import com.ruoyi.flow.core.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 全局SpringMVC Controller异常捕获
 *
 * @author wtian
 */
@ControllerAdvice
public class GlobalExceptionHandlerAdvice {

    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    @ExceptionHandler(NotFoundException.class)
    @ResponseBody
    public ExceptionResponse handleNotFound(NotFoundException e) {
        return createInfoFromException(e, HttpStatus.NOT_FOUND);
    }

    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    @ExceptionHandler(NonJsonResourceNotFoundException.class)
    public void handleNonJsonResourceNotFound(NonJsonResourceNotFoundException e) {
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    @ExceptionHandler(BadRequestException.class)
    @ResponseBody
    public ExceptionResponse handleBadRequest(BadRequestException e) {
        return createInfoFromException(e, HttpStatus.BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
    @ExceptionHandler(InternalServerErrorException.class)
    @ResponseBody
    public ExceptionResponse handleInternalServerError(InternalServerErrorException e) {
        return createInfoFromException(e, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ResponseStatus(HttpStatus.FORBIDDEN) // 403
    @ExceptionHandler(NotPermittedException.class)
    @ResponseBody
    public ExceptionResponse handleNoPermission(NotPermittedException e) {
        return createInfoFromException(e, HttpStatus.FORBIDDEN);
    }

    @ResponseStatus(HttpStatus.UNAUTHORIZED) // 401
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ExceptionResponse handleUnauthorized(UnauthorizedException e) {
        return createInfoFromException(e, HttpStatus.UNAUTHORIZED);
    }

    @ResponseStatus(HttpStatus.CONFLICT) // 409
    @ExceptionHandler(ConflictingRequestException.class)
    @ResponseBody
    public ExceptionResponse handleConflict(ConflictingRequestException e) {
        return createInfoFromException(e, HttpStatus.BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.REQUEST_ENTITY_TOO_LARGE) // 413
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public ExceptionResponse handleMaxFileSizeExceeded(MaxUploadSizeExceededException musee) {
        return ExceptionResponse.create(HttpStatus.REQUEST_ENTITY_TOO_LARGE.value(), "上传文件大小超限，最大 " + musee.getMaxUploadSize() + " byte");
    }

    protected ExceptionResponse createInfoFromException(BaseGuardException exception, HttpStatus status) {
        return ExceptionResponse.create(status.value(), exception.getMessage());
    }

    protected String getSafeMessageKey(String fragment) {
        if (fragment != null) {
            return fragment.toUpperCase();
        }
        return "";
    }

}
